﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

using nplJobsCommon.JobSeeker;
using System.Web.UI.HtmlControls;
using nplJobsBLL.ProcessSelectJobSeeker;
using nplJobsCommon.Employeer;
using nplJobsBLL.ProcessEmployeer;
using System.Text;
using nplJobsBLL.ProcessJobSeeker;
using nplJobsDAL.Common;
using System.Threading;
using nplJobsWebSite;
using System.Diagnostics;
using nplJobsBLL.Common;
using Elmah;

public partial class SiteMaster : MasterPage
{
    #region Events
    protected void ctlLogin_LoggedOut(object sender, EventArgs e)
    {
        Session["Logout"] = "Logout";
    }
    protected void Page_Init(object sender, EventArgs e)
    {
        Page.PreLoad += master_Page_PreLoad;
    }

    protected void master_Page_PreLoad(object sender, EventArgs e)
    {
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        headerhomenav1.Attributes.CssStyle.Add("display", "none");
        if (Page.AppRelativeVirtualPath.IndexOf("default.aspx", StringComparison.OrdinalIgnoreCase) > -1)
        {
            headerhomenav1.Attributes.CssStyle.Add("display", "block");
            headerhomenav.Attributes.CssStyle.Add("display", "none");
        }

        //Hide Sign up/Login link for Confirmation page
        if (Page.AppRelativeVirtualPath.Contains("Confirmation.aspx")
            || Page.AppRelativeVirtualPath.Contains("About.aspx")
             || Page.AppRelativeVirtualPath.Contains("AdvertisingTerms.aspx")
             || Page.AppRelativeVirtualPath.Contains("ContactUs.aspx")
             || Page.AppRelativeVirtualPath.Contains("PrivacyPolicy.aspx"))
        {
            lvUserLogIn.Visible = false;
        }
        if (Page.AppRelativeVirtualPath.Contains("NewUser.aspx") || Page.AppRelativeVirtualPath.Contains("ForgotPassword.aspx") || Page.AppRelativeVirtualPath.Contains("CategoryWiseJobSearch.aspx"))
        {
            lvUserLogIn.Visible = false;
        }
        if (Page.AppRelativeVirtualPath.Contains("JobDetails.aspx"))
        {
            //LoginStatus ls = lvUserLogIn.FindControl("ctlLogin") as LoginStatus;
            //if (ls != null)
            //{
            //    ls.Visible = false;
            //}
            Panel pnlAnonymousTemplate = lvUserLogIn.FindControl("pnlAnonymousTemplate") as Panel;
            if (pnlAnonymousTemplate != null)
            {
                pnlAnonymousTemplate.Visible = false;
            }
        }

        if (Session["user"] == null)
        {
            if (!IsPostBack)
            {
                LoginController();
                return;
            }
        }
        else
        {
            if (!Context.User.Identity.IsAuthenticated)
            {
                //Hide Sign up link for signup page
                if (Page.AppRelativeVirtualPath.Contains("NewUser.aspx"))
                {
                    lvUserLogIn.Visible = false;
                }
                if (Page.AppRelativeVirtualPath.Contains("JobSeeker/JobDetails.aspx"))
                {
                }
                if (Session["Logout"] != null && Session["Logout"].ToString() == "Logout")
                {
                    ClearSession();
                    FormsAuthentication.SignOut();
                    FormsAuthentication.RedirectToLoginPage();
                    ClearAuthCookJobSeeker();
                    ClearAuthCookEmp();

                    HttpCookie cookie1 = new HttpCookie(FormsAuthentication.FormsCookieName, "");
                    cookie1.Expires = DateTime.Now.AddYears(-1);
                    HttpContext.Current.Response.Cookies.Add(cookie1);
                    HttpContext.Current.Response.Cookies.Remove(FormsAuthentication.FormsCookieName);
                }
            }
            else
            {
                MembershipUser currentUser = Session["userInfo"] != null ? (MembershipUser)Session["userInfo"] : null;
                if (currentUser != null)
                {
                    Guid userID = (Guid)currentUser.ProviderUserKey;
                    LoginName loginName = lvUserLogIn.FindControl("lnUserLogin") as LoginName;

                    if (Page.AppRelativeVirtualPath.Contains("CategoryWiseJobSearch.aspx"))
                    {
                        divLogin.Visible = true;

                        loginName = lnUserLogin;
                        if (loginName != null)
                        {
                            loginName.FormatString = currentUser.UserName; //.Split(new[] { '@' })[0];
                            SetUserNameWithOutRedirect(currentUser, loginName, ancNavigate);
                        }
                    }
                    else
                    {
                        divLogin.Visible = false;

                        loginName.FormatString = currentUser.UserName; //.Split(new[] { '@' })[0];
                        if (!Page.AppRelativeVirtualPath.Contains("JobDetails.aspx"))
                        {
                            var anc = (HtmlAnchor)lvUserLogIn.FindControl("ancNavigate");
                            SetUserNameWithOutRedirect(currentUser, loginName, anc);
                        }

                    }
                    Session["UserID"] = userID;
                    if (Session["JOBSEEKERSUCCESSFULLLOGGED"] != null && Convert.ToBoolean(Session["JOBSEEKERSUCCESSFULLLOGGED"].ToString()))
                    {
                        string nurl = "../JobSeeker/EditProfile.aspx";
                        System.Web.HttpBrowserCapabilities browser = Request.Browser;
                        {
                            Response.Write("<script>");
                            Response.Write("var win = window.open('" + nurl + "', 'NewWindow');win.moveTo(0,0);win.resizeTo(window.screen.availWidth, window.screen.availHeight);");
                            Response.Write("<" + "/script>");
                        }
                        Session["JOBSEEKERSUCCESSFULLLOGGED"] = false;
                    }

                    if (Session["SUCCESSFULLLOGGED"] != null && Convert.ToBoolean(Session["SUCCESSFULLLOGGED"].ToString()))
                    {
                        string nurl = "../Employeer/EmployeerEntry.aspx";
                        System.Web.HttpBrowserCapabilities browser = Request.Browser;
                        {
                            Response.Write("<script>");
                            Response.Write("var win = window.open('" + nurl + "', 'NewWindow');win.moveTo(0,0);win.resizeTo(window.screen.availWidth, window.screen.availHeight);");
                            Response.Write("<" + "/script>");
                        }
                        Session["SUCCESSFULLLOGGED"] = false;
                    }
                }
                else
                {
                    LoadUserLink();
                }
            }
        }
    }
    #endregion

    #region LoadUserLink
    private void LoadUserLink()
    {
        UserType userType = new UserType();

        LoginName loginName = lvUserLogIn.FindControl("lnUserLogin") as LoginName;
        string email = Context.User.Identity.Name;
        loginName.FormatString = email; //.Split(new[] { '@' })[0];
        ProcessSelectUserTypeByEmail selectUserType = new ProcessSelectUserTypeByEmail();
        selectUserType.EMail = email;
        selectUserType.invoke();
        userType = selectUserType.UserType;
        if (userType != null)
        {
            MembershipUser userInfo = System.Web.Security.Membership.GetUser(Context.User.Identity.Name);

            Session["userInfo"] = userInfo;
            Session["user"] = userType.User;
            Session["UserID"] = userType.ID;

            if (userType.User == General.Users.JobSeeker.ToString())
            {
                UserType ut = userType;
                var anc = (HtmlAnchor)lvUserLogIn.FindControl("ancNavigate");
                anc.Target = "_blank";
                anc.HRef = "/JobSeeker/EditProfile.aspx";

                Guid id = Guid.Parse(userInfo.ProviderUserKey.ToString());
                JobSeeker jobSeeker = GetJobSeeker(id);
                if (jobSeeker != null)
                {
                    Session["jobseeker"] = jobSeeker;
                }
            }
            if (userType.User == General.Users.Employeer.ToString())
            {
                UserType ut = userType;
                var anc = (HtmlAnchor)lvUserLogIn.FindControl("ancNavigate");
                anc.Target = "_blank";
                anc.HRef = "/Employeer/EmployeerEntry.aspx";
            }
        }
    }
    #endregion

    #region SetUserName
    /// <summary>
    ///  Set user name
    /// </summary>
    /// <param name="currentUser"></param>
    /// <param name="loginName"></param>
    private void SetUserName(MembershipUser currentUser, LoginName loginName)
    {
        ProcessSelectUserTypeByEmail selectUserType = new ProcessSelectUserTypeByEmail();
        selectUserType.EMail = currentUser.UserName;
        selectUserType.invoke();
        UserType userType = selectUserType.UserType;

        var anc = (HtmlAnchor)lvUserLogIn.FindControl("ancNavigate");
        if (userType.User == General.Users.JobSeeker.ToString())
        {
            JobSeeker js = Session["jobseeker"] != null ? (JobSeeker)Session["jobseeker"] : null;
            loginName.FormatString = currentUser.UserName;
            anc.HRef = "/JobSeeker/EditProfile.aspx";
            anc.Target = "_blank";
            Response.Redirect("/JobSeeker/EditProfile.aspx");
        }
        else if (userType.User.ToString() == General.Users.Employeer.ToString())
        {
            loginName.FormatString = currentUser.UserName;//.Split('@')[0];
            anc.HRef = "/Employeer/EmployeerEntry.aspx";
            anc.Target = "_blank";
            Response.Redirect("/Employeer/EmployeerEntry.aspx");
        }
    }

    #endregion

    #region GetJobSeeker
    /// <summary>
    /// Get job seeker by id
    /// </summary>
    /// <param name="jobSeekerId"></param>
    /// <returns></returns>
    private JobSeeker GetJobSeeker(Guid jobSeekerId)
    {
        JobSeeker jobSeeker = new JobSeeker();
        ProcessSelectJobSeeker selectJobSeeker = new ProcessSelectJobSeeker();

        selectJobSeeker.JobSeekerID = jobSeekerId;
        selectJobSeeker.invoke();

        if (selectJobSeeker.JobSeeker != null)
        {
            jobSeeker = selectJobSeeker.JobSeeker.FirstOrDefault();
        }

        return jobSeeker;
    }
    #endregion

    #region JobSeeker Authentication
    protected void lnJobSeeker_Authenticate(object sender, AuthenticateEventArgs e)
    {
        TextBox txtEmail = lnJobSeeker.FindControl("UserName") as TextBox;
        string email = txtEmail.Text.Trim();
        Guid userID = Guid.Empty;
        try
        {
            JobSeekerLogIn(e, userID);
        }
        catch (ThreadAbortException ex)
        {
            throw ex;
        }
    }
    protected void lnJobSeeker_LoginError(object sender, EventArgs e)
    {
        lnJobSeeker.FailureText = "Your login attempt was not successful. Please try again.";
        MembershipUser usrInfo = System.Web.Security.Membership.GetUser(lnJobSeeker.UserName);
        TextBox txtJobSeekerPass = (TextBox)lnJobSeeker.FindControl("Password");
        txtJobSeekerPass.Attributes.Add("value", string.Empty);

        if (usrInfo != null)
        {
            if (usrInfo.IsLockedOut)
            {
                lnJobSeeker.FailureText = "Your account has been locked out because of too many invalid login attempts";
            }
            else if (!usrInfo.IsApproved)
            {
                lnJobSeeker.FailureText = "Your account has not yet been approved. You cannot login until you approve your account.";
            }
        }
    }

    #region JobSeekerLogIn
    private void JobSeekerLogIn(AuthenticateEventArgs e, Guid userID)
    {
        if (HttpContext.Current.Session["user"] != null)
            ClearSession();

        MembershipUser userInfo = System.Web.Security.Membership.GetUser(lnJobSeeker.UserName);
        System.Web.Security.MembershipUser currentUser = System.Web.Security.Membership.GetUser();

        if (userInfo != null)
        {
            userID = (Guid)userInfo.ProviderUserKey;
            //string userType = TypeOfUser.UserType.JobSeeker.ToString();

            if (!IsUserExists(userID, 1))
            {

                TextBox txtPassword = (TextBox)lnJobSeeker.FindControl("Password");
                if (txtPassword != null)
                {
                    txtPassword.Text = string.Empty;
                }
                return;
            }
        }
        if (System.Web.Security.Membership.ValidateUser(lnJobSeeker.UserName, lnJobSeeker.Password))
        {
            if (userInfo != null)
            {
                e.Authenticated = true;

                JobSeeker jobSeeker = GetJobSeeker(userID);
                if (jobSeeker != null)
                {
                    Session["UserID"] = jobSeeker.JobSeekerID;
                    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, lnJobSeeker.UserName, DateTime.Now, DateTime.Now.AddDays(1), false,
                    "JobSeekerData", FormsAuthentication.FormsCookiePath);

                    string encTicket = FormsAuthentication.Encrypt(ticket);
                    HttpCookie hc = new HttpCookie("AuthCookJobSeeker", encTicket);
                    hc.Expires = ticket.Expiration;
                    HttpContext.Current.Response.Cookies.Add(hc);

                    HttpContext.Current.Response.Cookies["JobSeekerUserName"].Value = lnJobSeeker.UserName;
                    HttpContext.Current.Response.Cookies["JobSeekerPassword"].Value = lnJobSeeker.Password;
                    bool isRemembered = lnJobSeeker.RememberMeSet;
                    HttpContext.Current.Response.Cookies["JobSeekerRememberMe"].Value = isRemembered.ToString();

                    CheckBox chkRememberMe = (CheckBox)lnJobSeeker.FindControl("RememberMe");
                    if (chkRememberMe.Checked)
                    {
                        HttpContext.Current.Response.Cookies["JobSeekerUserName"].Expires = DateTime.Now.AddDays(1);
                        HttpContext.Current.Response.Cookies["JobSeekerPassword"].Expires = DateTime.Now.AddDays(1);
                        HttpContext.Current.Response.Cookies["JobSeekerRememberMe"].Expires = DateTime.Now.AddDays(1);
                    }
                    else
                    {
                        HttpContext.Current.Response.Cookies["JobSeekerUserName"].Expires = DateTime.Now.AddDays(-1);
                        HttpContext.Current.Response.Cookies["JobSeekerPassword"].Expires = DateTime.Now.AddDays(-1);
                        HttpContext.Current.Response.Cookies["JobSeekerRememberMe"].Expires = DateTime.Now.AddDays(-1);
                    }

                    Session["MoveToHomePage"] = "false";
                    Session["userInfo"] = userInfo;
                    Session["jobseeker"] = jobSeeker;
                    Session["user"] = General.Users.JobSeeker.ToString();
                    Session["JOBSEEKERSUCCESSFULLLOGGED"] = true;
                }
                else
                {
                    ClearAuthCookJobSeeker();
                }
            }
            else
            {
                ClearAuthCookJobSeeker();
            }
        }
        else
        {
            ClearAuthCookJobSeeker();
        }
    }
    #endregion

    #region IsUserExists
    private bool IsUserExists(Guid userID, int userType)
    {
        bool isExists = false;
        try
        {
            ProcessSelectUserByID user = new ProcessSelectUserByID();
            user.UserID = userID;
            user.UserType = userType;
            user.invoke();
            int userValue = user.IsExists;

            isExists = userValue == userType ? true : false;
        }
        catch (Exception ex)
        {
            ErrorSignal.FromCurrentContext().Raise(ex);
        }

        return isExists;
    }
    #endregion

    #region ClearAuthCookJobSeeker
    private void ClearAuthCookJobSeeker()
    {
        if (Request.Cookies["AuthCookJobSeeker"] != null)
        {
            HttpCookie aCookie = Request.Cookies["AuthCookJobSeeker"];
            aCookie.Values.Remove("JobSeekerUserName");
            aCookie.Values.Remove("JobSeekerPassword");
            aCookie.Values.Remove("JobSeekerRememberMe");
            aCookie.Expires = DateTime.Now.AddDays(-1d);
            Response.Cookies.Add(aCookie);
        }
    }
    #endregion
    #endregion

    #region Employeer Authentication
    #region GetEmployeer
    /// <summary>
    /// Get employeer by id
    /// </summary>
    /// <param name="employeerId"></param>
    /// <returns></returns>
    private Employeer GetEmployeer(Guid employeerId)
    {
        Employeer employeer = new Employeer();
        ProcessSelectEmployeer selectEmployeer = new ProcessSelectEmployeer();

        selectEmployeer.employeerID = employeerId;
        selectEmployeer.invoke();

        if (selectEmployeer.Employeer != null)
        {
            employeer = selectEmployeer.Employeer.FirstOrDefault();
        }

        return employeer;
    }
    #endregion

    protected void lnEmployeer_Authenticate(object sender, AuthenticateEventArgs e)
    {
        Guid userID = Guid.Empty;
        try
        {
            EmployeerLogIn(e, userID);
        }
        catch (Exception ex)
        {
            ErrorSignal.FromCurrentContext().Raise(ex);
        }
    }
    private void EmployeerLogIn(AuthenticateEventArgs e, Guid userID)
    {
        if (HttpContext.Current.Session["user"] != null)
            ClearSession();

        MembershipUser userInfo = System.Web.Security.Membership.GetUser(lnEmployeer.UserName);
        System.Web.Security.MembershipUser currentUser = System.Web.Security.Membership.GetUser();

        if (userInfo != null)
        {
            userID = (Guid)userInfo.ProviderUserKey;
            string userType = TypeOfUser.UserType.Employeer.ToString();

            if (!IsUserExists(userID, 2))
            {
                TextBox txtPassword = (TextBox)lnEmployeer.FindControl("Password");
                if (txtPassword != null)
                {
                    txtPassword.Text = string.Empty;
                }

                return;
            }
        }
        if (System.Web.Security.Membership.ValidateUser(lnEmployeer.UserName, lnEmployeer.Password))
        {
            if (userInfo != null)
            {
                e.Authenticated = true;

                Employeer employeer = GetEmployeer(userID);
                if (employeer != null)
                {
                    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, lnEmployeer.UserName, DateTime.Now, DateTime.Now.AddDays(1), false,
                          "EmployeerData", FormsAuthentication.FormsCookiePath);
                    string encTicket = FormsAuthentication.Encrypt(ticket);
                    HttpCookie hc = new HttpCookie("AuthCookEmp", encTicket);
                    hc.Expires = ticket.Expiration;
                    HttpContext.Current.Response.Cookies.Add(hc);

                    HttpContext.Current.Response.Cookies["EmpUserName"].Value = lnEmployeer.UserName;
                    HttpContext.Current.Response.Cookies["EmpPassword"].Value = lnEmployeer.Password;

                    CheckBox chkRememberMe = (CheckBox)lnEmployeer.FindControl("RememberMe");
                    if (chkRememberMe != null)
                        HttpContext.Current.Response.Cookies["EmpRememberMe"].Value = chkRememberMe.Checked.ToString();
                    else
                        HttpContext.Current.Response.Cookies["EmpRememberMe"].Value = "";

                    if (chkRememberMe.Checked)
                    {
                        HttpContext.Current.Response.Cookies["EmpUserName"].Expires = DateTime.Now.AddDays(1);
                        HttpContext.Current.Response.Cookies["EmpPassword"].Expires = DateTime.Now.AddDays(1);
                        HttpContext.Current.Response.Cookies["EmpRememberMe"].Expires = DateTime.Now.AddDays(1);
                    }
                    else
                    {
                        HttpContext.Current.Response.Cookies["EmpUserName"].Expires = DateTime.Now.AddDays(-1);
                        HttpContext.Current.Response.Cookies["EmpPassword"].Expires = DateTime.Now.AddDays(-1);
                        HttpContext.Current.Response.Cookies["EmpRememberMe"].Expires = DateTime.Now.AddDays(-1);
                    }
                    Session["userInfo"] = userInfo;
                    Session["user"] = General.Users.Employeer.ToString();
                    //Session["employeer"] = employeer;
                    Session["SUCCESSFULLLOGGED"] = true;
                }
                else
                {
                    ClearAuthCookEmp();
                }
            }
            else
            {
                ClearAuthCookEmp();
            }
        }
        else
        {
            ClearAuthCookEmp();
        }
    }
    private void ClearAuthCookEmp()
    {
        if (Request.Cookies["AuthCookEmp"] != null)
        {
            HttpCookie aCookie = Request.Cookies["AuthCookEmp"];
            aCookie.Values.Remove("EmpUserName");
            aCookie.Values.Remove("EmpPassword");
            //aCookie.Values.Remove("EmpRememberMe");
            aCookie.Expires = DateTime.Now.AddDays(-1d);
            Response.Cookies.Add(aCookie);
        }
    }
    protected void lnEmployeer_LoginError(object sender, EventArgs e)
    {
        lnEmployeer.FailureText = "Your login attempt was not successful. Please try again.";
        MembershipUser usrInfo = System.Web.Security.Membership.GetUser(lnEmployeer.UserName);

        TextBox txtEmpPass = (TextBox)lnEmployeer.FindControl("Password");
        txtEmpPass.Attributes.Add("value", string.Empty);

        if (usrInfo != null)
        {
            if (usrInfo.IsLockedOut)
            {
                lnEmployeer.FailureText = "Your account has been locked out because of too many invalid login attempts. Please contact the administrator to have your account unlocked.";
            }
            else if (!usrInfo.IsApproved)
            {
                lnEmployeer.FailureText = "Your account has not yet been approved. You cannot login until an administrator has approved your account.";
            }
        }
    }
    #endregion

    #region LoginController
    private void LoginController()
    {
        if (Context.User.Identity.IsAuthenticated)
        {
            if (Session["user"] != null)
            {
                MembershipUser c = Session["userInfo"] != null ? (MembershipUser)Session["userInfo"] : null;
                if (Session["user"].ToString() == General.Users.JobSeeker.ToString())
                {
                    lnJobSeeker.DestinationPageUrl = "/JobSeeker/EditProfile.aspx";
                    LoginName loginName = lvUserLogIn.FindControl("lnUserLogin") as LoginName;
                    SetUserName(c, loginName);
                }
                else if (Session["user"].ToString() == General.Users.Employeer.ToString())
                {
                    lnJobSeeker.DestinationPageUrl = "/Employeer/EmployeerEntry.aspx";
                    LoginName loginName = lvUserLogIn.FindControl("lnUserLogin") as LoginName;
                    SetUserName(c, loginName);
                }
            }
            else
            {
                LoadUserLink();
            }
        }

        #region JobSeeker
        if (HttpContext.Current.Response.Cookies["AuthCookJobSeeker"] != null)
        {
            if (HttpContext.Current.Request.Cookies["JobSeekerUserName"] != null || HttpContext.Current.Request.Cookies["JobSeekerPassword"] != null)
            {
                string pass = HttpContext.Current.Request.Cookies["JobSeekerPassword"].Value;
                string userName = HttpContext.Current.Request.Cookies["JobSeekerUserName"].Value;

                if (lvUserLogIn.FindControl("lnJobSeeker") != null)
                {
                    System.Web.UI.WebControls.Login lnJobSeeker = (System.Web.UI.WebControls.Login)lvUserLogIn.FindControl("lnJobSeeker");
                    TextBox txtPass = (TextBox)lnJobSeeker.FindControl("Password");
                    txtPass.Attributes.Add("value", pass);

                    TextBox txtUserName = (TextBox)lnJobSeeker.FindControl("UserName");
                    txtUserName.Attributes.Add("value", userName);

                    bool isRemembered = !String.IsNullOrEmpty(HttpContext.Current.Request.Cookies["JobSeekerRememberMe"].Value) ? bool.Parse(HttpContext.Current.Request.Cookies["JobSeekerRememberMe"].Value.ToString()) : false;

                    //CheckBox chkRememberMe = (CheckBox)lnJobSeeker.FindControl("RememberMe");
                    //chkRememberMe.Checked= isRemembered;

                    lnJobSeeker.RememberMeSet = isRemembered;

                }
            }
            else
            {
                if (Session["Logout"] != null && Session["Logout"].ToString() == "Logout")
                {

                    ClearSession();
                    FormsAuthentication.SignOut();
                    FormsAuthentication.RedirectToLoginPage();

                    HttpCookie cookie1 = new HttpCookie(FormsAuthentication.FormsCookieName, "");
                    cookie1.Expires = DateTime.Now.AddYears(-1);
                    HttpContext.Current.Response.Cookies.Add(cookie1);
                    HttpContext.Current.Response.Cookies.Remove(FormsAuthentication.FormsCookieName);
                }
                ClearAuthCookJobSeeker();
            }
        }
        else
        {
            //JobSeeker
            TextBox txtJobSeekerUserName = (TextBox)lnJobSeeker.FindControl("UserName");
            txtJobSeekerUserName.Attributes.Add("value", string.Empty);
            TextBox txtJobSeekerPass = (TextBox)lnJobSeeker.FindControl("Password");
            txtJobSeekerPass.Attributes.Add("value", string.Empty);
            lnJobSeeker.RememberMeSet = false;
        }
        #endregion

        //Employeer
        #region Employeer
        if (HttpContext.Current.Response.Cookies["AuthCookEmp"] != null)
        {
            if (HttpContext.Current.Request.Cookies["EmpUserName"] != null || HttpContext.Current.Request.Cookies["EmpPassword"] != null)
            {
                string pass = HttpContext.Current.Request.Cookies["EmpPassword"].Value;
                string userName = HttpContext.Current.Request.Cookies["EmpUserName"].Value;

                if (lvUserLogIn.FindControl("lnEmployeer") != null)
                {
                    System.Web.UI.WebControls.Login lnEmployeer = (System.Web.UI.WebControls.Login)lvUserLogIn.FindControl("lnEmployeer");
                    TextBox txtPass = (TextBox)lnEmployeer.FindControl("Password");
                    txtPass.Attributes.Add("value", pass);

                    TextBox txtUserName = (TextBox)lnEmployeer.FindControl("UserName");
                    txtUserName.Attributes.Add("value", userName);

                    bool isRemembered = !String.IsNullOrEmpty(HttpContext.Current.Request.Cookies["EmpRememberMe"].Value) ? bool.Parse(HttpContext.Current.Request.Cookies["EmpRememberMe"].Value.ToString()) : false;
                    //CheckBox chkRememberMe = (CheckBox)lnEmployeer.FindControl("RememberMe");
                    //chkRememberMe.Checked = isRemembered;

                    lnEmployeer.RememberMeSet = isRemembered;
                }
            }

            else
            {
                if (Session["Logout"] != null && Session["Logout"].ToString() == "Logout")
                {
                    ClearSession();
                    FormsAuthentication.SignOut();
                    FormsAuthentication.RedirectToLoginPage();

                    HttpCookie cookie1 = new HttpCookie(FormsAuthentication.FormsCookieName, "");
                    cookie1.Expires = DateTime.Now.AddYears(-1);
                    HttpContext.Current.Response.Cookies.Add(cookie1);
                    HttpContext.Current.Response.Cookies.Remove(FormsAuthentication.FormsCookieName);
                }
                ClearAuthCookEmp();
            }
        }
        else
        {
            //Employeer
            TextBox txtEmpUserName = (TextBox)lnEmployeer.FindControl("UserName");
            txtEmpUserName.Attributes.Add("value", string.Empty);
            TextBox txtEmpPass = (TextBox)lnEmployeer.FindControl("Password");
            txtEmpPass.Attributes.Add("value", string.Empty);
            lnEmployeer.RememberMeSet = false;
        }
        #endregion
    }
    #endregion

    #region SetUserNameWithOutRedirect
    private void SetUserNameWithOutRedirect(MembershipUser currentUser, LoginName loginName,HtmlAnchor anc)
    {
        ProcessSelectUserTypeByEmail selectUserType = new ProcessSelectUserTypeByEmail();
        selectUserType.EMail = currentUser.UserName;
        selectUserType.invoke();
        UserType userType = selectUserType.UserType;

        if (userType.User == General.Users.JobSeeker.ToString())
        {
            JobSeeker js = Session["jobseeker"] != null ? (JobSeeker)Session["jobseeker"] : null;
            loginName.FormatString = currentUser.UserName;//.Split('@')[0];
            anc.Target = "_blank";
            anc.HRef = "/JobSeeker/EditProfile.aspx";
        }
        else if (userType.User.ToString() == General.Users.Employeer.ToString())
        {
            loginName.FormatString = currentUser.UserName;//.Split('@')[0];
            anc.Target = "_blank";
            anc.HRef = "/Employeer/EmployeerEntry.aspx";
        }
    }
    #endregion

    private void ClearSession()
    {
        Session.Clear();
        Session.Abandon();
        Session.RemoveAll();
    }

}
